package com.finger.forest.http;

import android.content.Context;

import com.finger.forest.R;
import com.finger.forest.base.BaseApplication;
import com.finger.forest.base.BaseView;
import com.finger.forest.base.ResponseTag;
import com.finger.forest.model.BaseModel;
import com.finger.forest.utils.NetUtil;
import com.finger.forest.utils.ToastUtils;

import java.io.File;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;

/**
 * 网络请求统一管理
 */
public class NetManager {
    private BaseView view;
    private Context context;
    private List<Call<Object>> callList = new ArrayList<>();

    public NetManager(Context context) {
        this.context = context;
//        subscription = new CompositeSubscription();
    }

    //网络请求失败,请稍后重试
    //public final static String NET_FAIL = BaseApplication.getInstance().getString(R.string.net_fail);
    //网络连接失败,请稍后重试
    //public final static String CONN_FAIL = BaseApplication.getInstance().getString(R.string.conn_fail);
    //证书验证失败
    //public final static String SSL_FAIL = BaseApplication.getInstance().getString(R.string.ssl_fail);
    //数据有误
    //public final static String DATA_FAIL = BaseApplication.getInstance().getString(R.string.data_fail);
    //获取数据失败
    //public final static String GET_DATA_FAIL = BaseApplication.getInstance().getString(R.string.get_data_fail);
    //TOKEN失效
    //public final static String TOKEN_INVALID = BaseApplication.getInstance().getString(R.string.token_invalid);

    public void setView(BaseView view) {
        this.view = view;
    }

    /**
     * 关闭
     */
    public void close() {
//        if (subscription.hasSubscriptions()) {
//            subscription.unsubscribe();
//        }
        for (Call<Object> call : callList) {
            if (!call.isCanceled() && !call.isExecuted()) {// 接口没有在执行，并且没有销毁
                call.cancel();
            }
        }
    }

    /**
     * 统一网络请求
     */
    public void requestDate(Call call, final BaseView view, final int tag) {
        try {
            if (NetUtil.isNetworkAvailable()) {// 判断当前网络是否可用
                if (callList != null) {
                    callList.add(call);
                }
                call.enqueue(new XPCallback() {
                    @Override
                    public void onSuccess(int code, BaseModel baseEntity) {
                        if (baseEntity.getCode() == 1) {
                            view.onSuccess(baseEntity.getRst(), tag, baseEntity.getMsg());
                        }

                        if (baseEntity.getCode() == 0) {
                            view.onError(baseEntity.getCode(), tag, baseEntity.getMsg());
                        }

                    }

                    @Override
                    public void onError(String baseEntity, Throwable t) {
                        view.onError(400, tag, baseEntity);
                    }
                });
            } else {// 提示当前无网络
                ToastUtils.show(context, context.getString(R.string.NoSignalException));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 用户登录
     */
    public void userLogin(String username, String password, String isonline) {
        requestDate(Api.getInstanceGson().userLogin(username, password, isonline), view, ResponseTag.USER_LOGIN);
    }

    /**
     * 首页用户消息列表
     */
    public void userNoticeList(Map<String, Object> maps) {
        requestDate(Api.getInstanceGson().userNoticeList(maps), view, ResponseTag.USER_MESSAGE_LIST);
    }

    /**
     * 首页用户消息已读
     */
    public void userNoticeRead(Map<String, Object> maps) {
        requestDate(Api.getInstanceGson().userNoticeRead(maps), view, ResponseTag.USER_MESSAGE_READ);
    }

    /**
     * 首页用户消息置顶
     */
    public void userNoticeTop(Map<String, Object> maps) {
        requestDate(Api.getInstanceGson().userNoticeTop(maps), view, ResponseTag.USER_MESSAGE_TOP);
    }

    /**
     * 首页用户消息删除
     */
    public void userNoticeDelete(Map<String, Object> maps) {
        requestDate(Api.getInstanceGson().userNoticeDelete(maps), view, ResponseTag.USER_MESSAGE_DELETE);
    }

    /**
     * 联系人删除
     */
    public void contactDelete(Map<String, Object> maps) {
        requestDate(Api.getInstanceGson().contactDelete(maps), view, ResponseTag.CONTACT_DELETE);
    }

    /**
     * 巡护上报
     */
    public void savePatrol(int patrolId, int type, String remark, BigDecimal longitude, BigDecimal latitude,
                           String findTime, String findAddress, File[] imageFile, File[] voiceFile, File[] videoFile) {
       /* requestDate(Api.getInstanceGson().savePatrol(patrolId, type, remark, longitude, latitude,
                findTime, findAddress, imageFile, voiceFile, videoFile), view, ResponseTag.SAVE_PATROL);*/
    }

    public void recognition(Map<String, RequestBody> bodyMap, MultipartBody.Part part) {
        requestDate(Api.getInstanceGson().recognition(bodyMap, part), view, ResponseTag.RECOGNITION);
    }

    public void recognitions(Map<String, RequestBody> bodyMap, List<MultipartBody.Part> part, List<MultipartBody.Part> part1, List<MultipartBody.Part> part2) {
        requestDate(Api.getInstanceGson().recognitions(bodyMap, part, part1, part2), view, ResponseTag.SAVE_PATROL);
    }

    /**
     * 发送验证码
     */
    public void sendSms(String phoneNum, int type) {
        requestDate(Api.getInstanceGson().loadLoginSmsCode(phoneNum, type), view, ResponseTag.SEND_CODE);
    }

    /**
     * 用户注册
     */
    public void userRegister(String cellphone, String password, String phoneCode, String cityCode) {
        requestDate(Api.getInstanceGson().userRegister(cellphone, password, phoneCode, cityCode), view, ResponseTag.USER_REGISTER);
    }

    /**
     * 忘记密码
     */
    public void forgetPassWord(String newPass, String phoneCode, String cellphone) {
        requestDate(Api.getInstanceGson().forgetPassWord(newPass, phoneCode, cellphone), view, ResponseTag.GORGET_PASSWORD);
    }

    /**
     * 提交工作日志
     */
    public void submitWorkLog(Map<String, Object> maps) {
        requestDate(Api.getInstanceGson().submitWorkLog(maps), view, ResponseTag.SUBMIT_WORK_LOG);
    }

    /**
     * 查询工作日志
     */
    public void queryWorkLog(Map<String, Object> maps) {
        requestDate(Api.getInstanceGson().queryWorkLog(maps), view, ResponseTag.QUERY_WORK_LOG);
    }

    /**
     * 道路信息采集roadInfoCollection
     */
    public void roadInformationInsert(Map<String, Object> maps) {
        requestDate(Api.getInstanceGson().roadInformationInsert(maps), view, ResponseTag.ROAD_INFO_COLLECTION);
    }

    /**
     * 新增火场资源信息接口fireInformationInsert
     */
    public void fireInformationInsert(Map<String, Object> maps) {
        requestDate(Api.getInstanceGson().fireInformationInsert(maps), view, ResponseTag.FIRE_INFOMATION_INSERT);
    }

    /**
     * 用户道路资源信息查询loadRoadinformation
     */
    public void loadRoadinformation(Map<String, Object> maps) {
        requestDate(Api.getInstanceGson().loadRoadinformation(maps), view, ResponseTag.ROAD_INFOMATION);
    }

    /**
     * 查询资源信息列表loadFireinformation
     */
    public void loadFireinformation(Map<String, Object> maps) {
        requestDate(Api.getInstanceGson().loadFireinformation(maps), view, ResponseTag.FIRE_INFOMATION);
    }

    /**
     * 查询用户所有资源信息列表loadResourceInformation
     */
    public void loadResourceInformation(Map<String, Object> maps) {
        requestDate(Api.getInstanceGson().loadResourceInformation(maps), view, ResponseTag.RESOURCE_INFOMATION);
    }

    /**
     * 上班打卡
     */
    public void submitOnSign(Map<String, Object> maps) {
        requestDate(Api.getInstanceGson().submitOnSign(maps), view, ResponseTag.SUBMIT_ON_SIGN);
    }

    /**
     * 查詢個人信息loadPersonInfo
     */
    public void loadPersonInfo(Map<String, Object> maps) {
        requestDate(Api.getInstanceGson().loadPersonInfo(maps), view, ResponseTag.CHECK_PERSON_INFO);
    }

    /**
     * 获取资源类型loadResourceType
     */
    public void loadResourceType() {
        requestDate(Api.getInstanceGson().loadResourceType(), view, ResponseTag.RESOURCE_TYPE);
    }


    /**
     * 用户管理loadUserManager
     */
    public void loadUserManager(Map<String, Object> maps) {
        requestDate(Api.getInstanceGson().loadUserManager(maps), view, ResponseTag.USER_MANAGER);
    }

    /**
     * 编辑（删除）用户deleteUser
     */
    public void deleteUser(Map<String, Object> maps) {
        requestDate(Api.getInstanceGson().deleteUser(maps), view, ResponseTag.DELETE_USER);
    }

    /**
     * 查询通讯信息voiceintercomByGrui
     */
    public void voiceintercomByGrui(Map<String, Object> maps) {
        requestDate(Api.getInstanceGson().voiceintercomByGrui(maps), view, ResponseTag.CHECK_VOICE_LIST);
    }

    /**
     * 新增通讯消息insertVoiceintercom
     */
    public void insertVoiceintercom(Map<String, RequestBody> bodyMap, MultipartBody.Part part) {
        requestDate(Api.getInstanceGson().insertVoiceintercom(bodyMap, part), view, ResponseTag.INSERT_VOICE_MESSAGE);
    }

    /**
     * 下班打卡
     */
    public void submitOffSign(Map<String, Object> maps) {
        requestDate(Api.getInstanceGson().submitOffSign(maps), view, ResponseTag.SUBMIT_OFF_SIGN);
    }

    /**
     * 查询打卡记录
     */
    public void querySignByDate(Map<String, Object> maps) {
        requestDate(Api.getInstanceGson().querySignByDate(maps), view, ResponseTag.QUERY_SIGN_RECORD);
    }

    /**
     * 查询热点核查
     */
    public void queryHotList(Map<String, Object> maps) {
        requestDate(Api.getInstanceGson().queryHotList(maps), view, ResponseTag.QUERY_HOT_LIST);
    }

    /**
     * 查询热点核查详情
     */
    public void queryHotDetail(Map<String, Object> maps) {
        requestDate(Api.getInstanceGson().queryHotDetail(maps), view, ResponseTag.QUERY_HOT_DETAIL);
    }

    /**
     * 查询报警详情
     */
    public void queryAlarmDetail(Map<String, Object> maps) {
        requestDate(Api.getInstanceGson().queryAlarmDetail(maps), view, ResponseTag.QUERY_ALARM_DETAIL);
    }

    /**
     * 查询卡口详情
     */
    public void queryBayonetDetail(Map<String, Object> maps) {
        requestDate(Api.getInstanceGson().queryBayonetDetail(maps), view, ResponseTag.QUERY_Bayonet_DETAIL);
    }

    /**
     * 查询基站详情
     */
    public void queryStationDetail(Map<String, Object> maps) {
        requestDate(Api.getInstanceGson().queryStationDetail(maps), view, ResponseTag.QUERY_STATION_DETAIL);
    }

    /**
     * 核查反馈
     *
     * @param bodyMap
     * @param part
     * @param part1
     * @param part2
     */
    public void checkFeedBack(Map<String, RequestBody> bodyMap, List<MultipartBody.Part> part, List<MultipartBody.Part> part1, List<MultipartBody.Part> part2) {
        requestDate(Api.getInstanceGson().checkFeedBack(bodyMap, part, part1, part2), view, ResponseTag.CHECK_FEED_BACK);
    }

    /**
     * 查询个人详情
     */
    public void getUserDetail(Map<String, Object> maps) {
        requestDate(Api.getInstanceGson().getUserDetail(maps), view, ResponseTag.GET_USER_DETAIL);
    }

    /**
     * 查询GPS设备
     */
    public void queryGPSList(Map<String, Object> maps) {
        requestDate(Api.getInstanceGson().queryGPSList(maps), view, ResponseTag.QUERY_GPS_LIST);
    }

    /**
     * 查询车辆设备
     */
    public void queryCarList(Map<String, Object> maps) {
        requestDate(Api.getInstanceGson().queryCarList(maps), view, ResponseTag.QUERY_CAR_LIST);
    }

    /**
     * 查询热点核查
     */
    public void queryAlarmList(Map<String, Object> maps) {
        requestDate(Api.getInstanceGson().queryAlarmList(maps), view, ResponseTag.QUERY_ALARM_LIST);
    }

    /**
     * 查询卡口
     */
    public void queryBayonetList(Map<String, Object> maps) {
        requestDate(Api.getInstanceGson().queryBayonetList(maps), view, ResponseTag.QUERY_Bayonet_LIST);
    }

    /**
     * 查询基站
     */
    public void queryStationList(Map<String, Object> maps) {
        requestDate(Api.getInstanceGson().queryStationList(maps), view, ResponseTag.QUERY_STATION_LIST);
    }

    /**
     * 报警核查反馈
     *
     * @param bodyMap
     * @param part
     * @param part1
     * @param part2
     */
    public void alarmCheckFeedBack(Map<String, RequestBody> bodyMap, List<MultipartBody.Part> part, List<MultipartBody.Part> part1, List<MultipartBody.Part> part2) {
        requestDate(Api.getInstanceGson().alarmCheckFeedBack(bodyMap, part, part1, part2), view, ResponseTag.ALARM_CHECK_FEED_BACK);
    }

    /**
     * 新增用户信息insertUserInfo
     *
     * @param bodyMap
     * @param part
     */
    public void insertUserInfo(Map<String, RequestBody> bodyMap, MultipartBody.Part part) {
        requestDate(Api.getInstanceGson().insertUserInfo(bodyMap, part), view, ResponseTag.INSERT_USER_INFO);
    }

    /**
     * sos
     *
     * @param bodyMap
     * @param part
     */
    public void uploadSos(Map<String, RequestBody> bodyMap, List<MultipartBody.Part> part) {
        requestDate(Api.getInstanceGson().uploadSos(bodyMap, part), view, ResponseTag.UPLOAD_SOS);
    }

    /**
     * 上传资源采集uploadResource
     *
     * @param bodyMap
     * @param part
     */
    public void uploadResource(Map<String, RequestBody> bodyMap, List<MultipartBody.Part> part, List<MultipartBody.Part> part1) {
        requestDate(Api.getInstanceGson().uploadResource(bodyMap, part, part1), view, ResponseTag.UPLOAD_RESOURCE);
    }

    /**
     * 查询巡护轨迹
     */
    public void queryPatrolTrail(Map<String, Object> maps) {
        requestDate(Api.getInstanceGson().queryPatrolTrail(maps), view, ResponseTag.QUERY_PATRAL_TRAIL);
    }

    /**
     * 新增巡护记录
     */
    public void addPatrolRecord(Map<String, Object> maps) {
        requestDate(Api.getInstanceGson().addPatrolRecord(maps), view, ResponseTag.ADD_PATRAL_RECORD);
    }

    /**
     * 新增联系人
     */
    public void addContact(Map<String, Object> maps) {
        requestDate(Api.getInstanceGson().addContact(maps), view, ResponseTag.ADD_CONTACT);
    }

    /**
     * 查询巡护发现
     */
    public void queryPatrolFind(Map<String, Object> maps) {
        requestDate(Api.getInstanceGson().queryPatrolFind(maps), view, ResponseTag.QUERY_PATRAL_FIND);
    }

    /**
     * 查询巡护发现
     */
    public void queryPatrolInfo(Map<String, Object> maps) {
        requestDate(Api.getInstanceGson().queryPatrolInfo(maps), view, ResponseTag.QUERY_PATRAL_INFO);
    }

    /**
     * 更新巡护记录
     */
    public void updatePatrolInfo(Map<String, Object> maps) {
        requestDate(Api.getInstanceGson().updatePatrolInfo(maps), view, ResponseTag.UPDATE_PATRAL_INFO);
    }

    /**
     * 查询道路详情
     */
    public void queryRoadDetail(Map<String, Object> maps) {
        requestDate(Api.getInstanceGson().queryRoadDetail(maps), view, ResponseTag.QUERY_ROAD_DETAIL);
    }

    /**
     * 查询火场详情
     */
    public void queryFireDetail(Map<String, Object> maps) {
        requestDate(Api.getInstanceGson().queryFireDetail(maps), view, ResponseTag.QUERY_FIRE_DETAIL);
    }

    /**
     * 查询资源详情
     */
    public void queryResourceDetail(Map<String, Object> maps) {
        requestDate(Api.getInstanceGson().queryResourceDetail(maps), view, ResponseTag.QUERY_RESOURCE_DETAIL);
    }

    /**
     * 查询联系人列表
     */
    public void queryContactList(Map<String, Object> maps) {
        requestDate(Api.getInstanceGson().queryContactList(maps), view, ResponseTag.QUERY_CONTACT_LIST);
    }

}
